﻿
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Data.SqlClient
Imports System.Text
Imports System.Data
Imports System.Web.UI.WebControls


Partial Class WebForm_PopForm_PopupForm
    Inherits BasePage

    ''' <summary>
    ''' 設定資料表名稱
    ''' </summary>
    ''' <value></value>
    ''' <returns>設定資料表名稱</returns>
    ''' <remarks>設定資料表名稱</remarks>
    Public Property TableName() As String
        Get
            If ViewState("TableName") Is Nothing Then
                ViewState("TableName") = ""
            End If

            Return ViewState("TableName")
        End Get
        Set(ByVal value As String)
            ViewState("TableName") = value
        End Set
    End Property

    Public Property dbTable() As DataTable
        Get
            If ViewState("dbTable") Is Nothing Then
                Return Nothing
            End If

            Return ViewState("dbTable")
        End Get
        Set(ByVal value As DataTable)
            ViewState("dbTable") = value
        End Set
    End Property

    '''' <summary>
    '''' 設定資料庫代號
    '''' </summary>
    '''' <value></value>
    '''' <returns>設定資料庫代號</returns>
    '''' <remarks>設定資料庫代號</remarks>
    'Public Property DbName() As String
    '    Get
    '        If ViewState("dbName") Is Nothing Then
    '            ViewState("dbName") = ""
    '        End If

    '        Return ViewState("dbName")
    '    End Get
    '    Set(ByVal value As String)
    '        ViewState("dbName") = value
    '    End Set
    'End Property



    ''' <summary>
    ''' 設定秀出資料的中文(HeaderText)欄位名稱
    ''' </summary>
    ''' <value></value>
    ''' <returns>設定秀出資料的中文(HeaderText)欄位名稱</returns>
    ''' <remarks>設定秀出資料的中文(HeaderText)欄位名稱</remarks>
    Public Property ChnFieldNames() As String
        Get
            If ViewState("ChnFieldNames") Is Nothing Then
                ViewState("ChnFieldNames") = ""
            End If

            Return ViewState("ChnFieldNames")
        End Get
        Set(ByVal value As String)
            ViewState("ChnFieldNames") = value
        End Set
    End Property

    ''' <summary>
    ''' 設定秀出資料的欄位名稱
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property FieldsName() As String
        Get
            If ViewState("FieldsName") Is Nothing Then
                ViewState("FieldsName") = ""
            End If

            Return ViewState("FieldsName")
        End Get
        Set(ByVal value As String)
            ViewState("FieldsName") = value
        End Set
    End Property


    ''' <summary>
    ''' 設定要回傳的欄位代號如:1,3欄位代號中間以','分開
    ''' </summary>
    ''' <value></value>
    ''' <returns>設定要回傳的欄位代號如:1,3欄位代號中間以','分開</returns>
    ''' <remarks>設定要回傳的欄位代號如:1,3欄位代號中間以','分開</remarks>
    Public Property ReturnFields() As String
        Get
            If ViewState("ReturnFields") Is Nothing Then
                ViewState("ReturnFields") = ""
            End If

            Return ViewState("ReturnFields")
        End Get
        Set(ByVal value As String)
            ViewState("ReturnFields") = value
        End Set
    End Property

    ''' <summary>
    ''' 設定回傳的webControl.ClientID中間以','分開
    ''' </summary>
    ''' <value></value>
    ''' <returns>設定回傳的webControl.ClientID中間以','分開</returns>
    ''' <remarks>設定回傳的webControl.ClientID中間以','分開</remarks>
    Public Property ReturnControls() As String
        Get
            If ViewState("ReturnControls") Is Nothing Then
                ViewState("ReturnControls") = ""
            End If

            Return ViewState("ReturnControls")
        End Get
        Set(ByVal value As String)
            ViewState("ReturnControls") = value
        End Set
    End Property
    ''' <summary>
    ''' 查詢鍵
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>查詢鍵</remarks>
    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        ' gvMain.Columns.Clear()
        Dim col1 As BoundField
        Dim Fields() As String = FieldsName.Split(",") '取出欄位名稱
        Dim chnFields() As String = ChnFieldNames.Split(",") '取出HeaderText設定值
        If gvMain.Columns.Count < 2 Then '尚未查詢過'加入要秀出的資料欄位
            For ii As Integer = 0 To Fields.Length - 1
                col1 = New BoundField()
                col1.DataField = Fields(ii)
                col1.HeaderText = chnFields(ii)
                gvMain.Columns.Add(col1)
            Next
        End If

        gvMain.SelectedIndex = -1 '清除已選定的列數
        gvMain.DataSource = getData() '查詢所設定條件的資料
        gvMain.DataBind()
        btnSelect.Visible = False '選定離開取消功能

    End Sub




    ''' <summary>
    ''' 取得連線字串
    ''' </summary>
    ''' <returns>取得連線字串</returns>
    ''' <remarks>取得連線字串</remarks>
    Public Function MyConn() As SqlConnection
        Dim myConn__1 As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings(DbName).ConnectionString)
        Return myConn__1
    End Function
    ''' <summary>
    ''' 查詢所設定條件的資料
    ''' </summary>
    ''' <returns>查詢所設定條件的資料</returns>
    ''' <remarks>查詢所設定條件的資料</remarks>
    Protected Function getData() As DataTable

        If TableName = "" Then
            Return dbTable
        End If

        Dim sb As New StringBuilder()
        sb.AppendLine("Select " & FieldsName & " from  " & TableName)
        If txtValue.Text.Trim <> "" Then
            sb.AppendLine(" where " & ddlField.SelectedValue & " like '%" & txtValue.Text & "%'")
        End If
        Return SqlHelper.ExecuteDataset(MyConn(), CommandType.Text, sb.ToString()).Tables(0)


    End Function
    ''' <summary>
    ''' 按下關閉鍵
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>按下關閉鍵</remarks>
    Protected Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        'window.opener.document.getElementById

        Dim sb As New StringBuilder()

        sb.AppendLine("<script>")
        sb.Append("window.close();")
        sb.Append("</script>" + vbCrLf)

        ScriptManager.RegisterStartupScript(btnClose, GetType(BasePage), "closeForm", sb.ToString(), False)

    End Sub
    ''' <summary>
    ''' 初始化設定
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>初始化設定</remarks>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ' DbName = Session("dbName").ToString()
            TableName = Session("TableName").ToString()
            ChnFieldNames = Session("ChnFieldNames").ToString()
            FieldsName = Session("FieldsName").ToString()
            ReturnFields = Session("ReturnFields").ToString()
            ReturnControls = Session("ReturnControls").ToString()
            If Not Session("dbTable") Is Nothing Then
                dbTable = Session("dbTable")
                btnQuery_Click(Nothing, Nothing)
                'gvMain.Visible = True
                'gvMain.DataSource = getData()
                'gvMain.DataBind()
            End If

            Dim Fields() As String = FieldsName.Split(",")
            Dim chnFields() As String = ChnFieldNames.Split(",")
            Dim li As ListItem
            For ii As Integer = 0 To Fields.Length - 1 ''設定欄位選項的內容
                li = New ListItem(chnFields(ii), Fields(ii))
                ddlField.Items.Add(li)

            Next
            If Not Session("PopMsg") Is Nothing Then
                lblMsg.Visible = True
                lblMsg.Text = Session("PopMsg")
                Session.Remove("PopMsg")
            End If
        End If
    End Sub
    ''' <summary>
    ''' 按選定離開
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>按選定離開</remarks>
    Protected Sub btnSelect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSelect.Click
        If gvMain.SelectedIndex < 0 Then ''沒有選定不可以按此按鍵,目前程式已有控制.為預防萬一
            ShowMessageForAjax(btnSelect, GetLocalResourceObject("errEmpty"))
        End If
        Dim sb As New StringBuilder()

        sb.AppendLine("<script>")


        Dim reFields() As String = ReturnFields.Split(",")
        Dim reControls() As String = ReturnControls.Split(",")
        'window.opener.document.getElementById
        For ii As Integer = 0 To reFields.Length - 1 '設定要回傳值欄位名稱等相關的JavaScript
            sb.AppendLine("try  { window.opener.document.getElementById('" & reControls(ii) & "').value='" & gvMain.Rows(gvMain.SelectedIndex).Cells(Integer.Parse(reFields(ii))).Text.Replace("'", "") & "';} catch(err) {};")
        Next
        sb.Append("window.close();")
        sb.Append("</script>" + vbCrLf)
        ScriptManager.RegisterStartupScript(btnClose, GetType(BasePage), "closeForm", sb.ToString(), False)

    End Sub

    Protected Sub gvMain_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvMain.SelectedIndexChanged
        If gvMain.SelectedIndex > -1 Then '換頁時取消選定的列數
            btnSelect.Visible = True
        End If

    End Sub
End Class
